package com.wut.clock.event;

import com.alibaba.fastjson.JSONObject;
import com.wut.clock.common.constant.ClockConstant;
import com.wut.clock.common.util.IpUtil;
import com.wut.clock.entity.Message;
import lombok.RequiredArgsConstructor;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 评论事件生产者
 * @date 2022/6/10 16:14
 */
@Component
@RequiredArgsConstructor
@SuppressWarnings("unchecked")
public class EventProducer {

    private final KafkaTemplate kafkaTemplate;

    // 处理评论事件
    public void fireCommentEvent(Message message){
        kafkaTemplate.send(ClockConstant.COMMENT_TOPIC,JSONObject.toJSONString(message));
    }

    // 处理点赞事件
    public void fireLikeEvent(Message message){
        kafkaTemplate.send(ClockConstant.LIKE_TOPIC, JSONObject.toJSONString(message));
    }

    //处理点踩事件
    public void fireDislikeEvent(Message message) {
        kafkaTemplate.send(ClockConstant.LIKE_TOPIC,JSONObject.toJSONString(message));
    }

    // 处理关注事件
    public void fireFollowEvent(Message message){
        kafkaTemplate.send(ClockConstant.FOLLOW_TOPIC,JSONObject.toJSONString(message));
    }

    // 处理收藏事件
    public void fireCollectionEvent(Message message){
        kafkaTemplate.send(ClockConstant.COLLECTION_TOPIC,JSONObject.toJSONString(message));
    }

    //处理文章浏览量自增
    public void fireArticleViewEvent(HttpServletRequest request,Integer articleId) {
        Map<String,Object> map = new HashMap<>();
        String ip = IpUtil.getIpAddr(request);
        map.put("ip",ip);
        map.put("articleId",articleId);
        kafkaTemplate.send(ClockConstant.ARTICLE_VIEW_TOPIC,JSONObject.toJSONString(map));
    }


}
